package com.ht.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ht.domain.bo.TbDoctorBO;
import com.ht.domain.bo.TbHospitalBO;
import com.ht.domain.bo.TbKnowledgeBO;
import com.ht.domain.bo.TbRelevantBO;
import com.ht.domain.po.*;
import com.ht.mapper.*;
import com.ht.service.KnowledgeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 知识表Service
 */
@Service
public class KnowledgeServiceImpl implements KnowledgeService {

    //知识表持久层
    @Autowired
    private KnowledgeMapper knowledgeMapper;

    //医院表持久层
    @Autowired
    private HospitalMapper hospitalMapper;

    //医院and知识中间表持久层
    @Autowired
    private HospitalKnowledgeMapper hospitalKnowledgeMapper;

    //医生表持久层
    @Autowired
    private DoctorMapper doctorMapper;

    //医生and知识中间表持久层
    @Autowired
    private DoctorKnowledgeMapper doctorKnowledgeMapper;

    //相关表持久层
    @Autowired
    private RelevantMapper relevantMapper;

    //知识and相关中间表持久层
    @Autowired
    private KnowledgeRelevantMapper knowledgeRelevantMapper;

    //医院类型
    @Autowired
    private HospitalTypeMapper hospitalTypeMapper;

    //科室表持久层
    @Autowired
    private DepartmentMapper departmentMapper;

    //职称表持久层
    @Autowired
    private PositionMapper positionMapper;

    //职位表持久层
    @Autowired
    private LevelMapper levelMapper;

    //医生排班表持久层
    @Autowired
    private ScheduleMapper scheduleMapper;

    /**
     * 根据知识类别id查询前八条数据
     * @param categoryId 知识id
     * @param chargeStatus 是否收费
     * @return
     */
    @Override
    public List<TbKnowledgeBO> findListByCategoryId(Long categoryId,Integer chargeStatus) {
        Page<TbKnowledge> knowledgePage = knowledgeMapper.selectPage(new Page<>(0, 8),
                                                            new QueryWrapper<TbKnowledge>().eq("category_id", categoryId)
                                                                                            .eq(chargeStatus != null,"charge_status",chargeStatus));
        List<TbKnowledge> knowledgeList = knowledgePage.getRecords();
        List<TbKnowledgeBO> tbKnowledgeBOList = new ArrayList<>();
        for (TbKnowledge knowledge:knowledgeList) {
            TbKnowledgeBO tbKnowledgeBO = new TbKnowledgeBO(knowledge);
            tbKnowledgeBOList.add(tbKnowledgeBO);
        }
        return tbKnowledgeBOList;
    }

    /**
     * 根据疾病,疫苗,检查,治疗名称进行模糊查询
     * @param knowledgeName
     * @return
     */
    @Override
    public List<TbKnowledgeBO> findListByName(String knowledgeName) {
        List<TbKnowledge> knowledgeList = knowledgeMapper.selectList(new QueryWrapper<TbKnowledge>()
                                                                    .like("knowledge_name", knowledgeName));
        List<TbKnowledgeBO> tbKnowledgeBOList = new ArrayList<>();
        for (TbKnowledge knowledge: knowledgeList) {
            TbKnowledgeBO tbKnowledgeBO = new TbKnowledgeBO(knowledge);
            tbKnowledgeBOList.add(tbKnowledgeBO);
        }
        return tbKnowledgeBOList;
    }

    /**
     * 根据专科id查询对应的疾病
     * @param juniorId
     * @return
     */
    @Override
    public List<TbKnowledgeBO> findListByJuniorId(Long juniorId) {
        List<TbKnowledge> knowledgeList = knowledgeMapper.selectList(new QueryWrapper<TbKnowledge>()
                                                                    .eq("junior_id", juniorId));
        List<TbKnowledgeBO> tbKnowledgeBOList = new ArrayList<>();
        for (TbKnowledge knowledge: knowledgeList) {
            TbKnowledgeBO tbKnowledgeBO = new TbKnowledgeBO(knowledge);
            tbKnowledgeBOList.add(tbKnowledgeBO);
        }
        return tbKnowledgeBOList;
    }

    /**
     * 根据知识id和市id还有医院等级,医院类型推荐医院
     * @param knowledgeId
     * @param cityId
     * @param hospitalGrade
     * @param hospitalTypeId
     * @return
     */
    @Override
    public List<TbHospitalBO> findHospitalListByKnowledgeId(Long knowledgeId, Long cityId,Integer hospitalGrade,Long hospitalTypeId) {
        List<TbHospitalKnowledge> tbHospitalKnowledges = hospitalKnowledgeMapper.selectList(new QueryWrapper<TbHospitalKnowledge>()
                                                                                        .eq("knowledge_id", knowledgeId));
        List<Long> hospitalIds = new ArrayList<>();
        for (TbHospitalKnowledge hospitalKnowledge:tbHospitalKnowledges) {
            hospitalIds.add(hospitalKnowledge.getHospitalId());
        }
        List<TbHospital> hospitalList = hospitalMapper.selectList(new QueryWrapper<TbHospital>().in("id", hospitalIds)
                .eq(cityId != null, "ctiy_id", cityId)
                .eq(hospitalGrade != null, "hospital_grade", hospitalGrade)
                .eq(hospitalTypeId != null, "hospital_type", hospitalTypeId)
                .eq("hospital_status",1));
        List<TbHospitalBO> hospitalBOList = new ArrayList<>();
        for (TbHospital hospital:hospitalList) {
            TbHospitalBO hospitalBO = new TbHospitalBO();
            BeanUtils.copyProperties(hospital,hospitalBO);
            //医院等级
            if (hospital.getHospitalGrade() == 1){
                hospitalBO.setHospitalGradeName("一甲");
            }else if (hospital.getHospitalGrade() == 2){
                hospitalBO.setHospitalGradeName("二甲");
            }else {
                hospitalBO.setHospitalGradeName("三甲");
            }
            //医院性质
            if ("0".equals(hospital.getHospitalNature())){
                hospitalBO.setHospitalNatureName("公立");
            }else {
                hospitalBO.setHospitalNatureName("私立");
            }
            //医院类型
            TbHospitalType tbHospitalType = hospitalTypeMapper.selectById(hospital.getHospitalType());
            hospitalBO.setHospitalTypeName(tbHospitalType.getTypeName());
            hospitalBOList.add(hospitalBO);
        }
        return hospitalBOList;
    }

    /**
     *根据知识id,市id,职位id,医院等级,科室id,医生坐诊日期推荐医生
     * @param knowledgeId 知识id
     * @param cityId 市id
     * @param levelId 职位id
     * @param hospitalGrade 医院等级
     * @param departmentId 科室id
     * @param workDate 医生坐诊日期
     * @return
     */
    @Override
    public List<TbDoctorBO> findDoctorListByKnowledgeId(Long knowledgeId, Long cityId,Long levelId,Integer hospitalGrade,Long departmentId,String workDate) {
        List<TbDoctor> doctorList = knowledgeMapper.findDoctorListByKnowledgeId(knowledgeId,cityId,levelId,hospitalGrade,departmentId);

        List<TbDoctorBO> doctorBOList = new ArrayList<>();
        for (TbDoctor doctor:doctorList) {
            //判断日期是否为null
            if (!StrUtil.isBlank(workDate)){
                List<TbSchedule> scheduleList = scheduleMapper.selectList(new QueryWrapper<TbSchedule>().eq("doctor_id", doctor.getId()).eq("work_date", workDate));
                if (scheduleList.size() <= 0 ){
                    continue;
                }
            }
            TbDoctorBO doctorBO = new TbDoctorBO();
            BeanUtils.copyProperties(doctor,doctorBO);
            //查询医院名称
            TbHospital hospital = hospitalMapper.selectById(doctor.getHospitalId());
            doctorBO.setHospitalName(hospital.getHospitalName());
            //查询科室名称
            TbDepartment department = departmentMapper.selectById(doctor.getDepartmentId());
            doctorBO.setDepartmentName(department.getDepartmentName());
            //查询职称
            TbPosition position = positionMapper.selectById(doctor.getPosition());
            doctorBO.setPositionName(position.getPosition());
            //查询职位
            TbLevel level = levelMapper.selectById(doctor.getLevel());
            doctorBO.setLevelname(level.getLevel());
            //判断是否开通问诊
            if (doctor.getInquiryStatus() == 1){
                doctorBO.setInquiryStatusName("开通");
            }else{
                doctorBO.setInquiryStatusName("未开通");
            }
            //是否能成为私人医生
            if (doctor.getPrivateStatus() == 0){
                doctorBO.setPrivateStatusName("可以成为私人医院");
            }else {
                doctorBO.setPrivateStatusName("不可以成为私人医院");
            }
            doctorBOList.add(doctorBO);
        }
        return doctorBOList;
    }

    /**
     * 根据知识id查询知识相关信息
     * @param knowledgeId 知识id
     * @return
     */
    @Override
    public List<TbRelevantBO> findRelevantListByKnowledgeId(Long knowledgeId) {
        List<TbKnowledgeRelevant> knowledgeRelevants = knowledgeRelevantMapper.selectList(new QueryWrapper<TbKnowledgeRelevant>().eq("knowledge_id", knowledgeId));
        List<TbRelevantBO> relevantBOList = new ArrayList<>();
        for (TbKnowledgeRelevant knowledgeRelevant:knowledgeRelevants) {
            TbRelevant tbRelevant = relevantMapper.selectById(knowledgeRelevant.getRelevantId());
            TbRelevantBO relevantBO = new TbRelevantBO();
            BeanUtils.copyProperties(tbRelevant,relevantBO);
            relevantBOList.add(relevantBO);
        }
        return relevantBOList;
    }

    /**
     * 根据医院id和知识id查询医生
     * @param knowledgeId 知识id
     * @param hospitalId 医院id
     * @return
     */
    @Override
    public List<TbDoctorBO> findDoctorListByHospitalIdAndknowledgeId(Long knowledgeId, Long hospitalId) {
        List<TbDoctor> doctorList = doctorMapper.selectList(new QueryWrapper<TbDoctor>().eq("hospital_id", hospitalId)
                                                                                        .eq("audit_status",1));
        List<TbDoctorBO> doctorBOList = new ArrayList<>();
        for (TbDoctor doctor:doctorList) {
            List<TbDoctorKnowledge> doctorKnowledgeList = doctorKnowledgeMapper.selectList(new QueryWrapper<TbDoctorKnowledge>()
                                                                                            .eq("doctor_id", doctor.getId())
                                                                                            .eq("knowledge_id", knowledgeId));
            if (doctorKnowledgeList.size() <= 0){
                continue;
            }
            TbDoctorBO doctorBO = new TbDoctorBO();
            BeanUtils.copyProperties(doctor,doctorBO);
            //查询医院名称
            TbHospital hospital = hospitalMapper.selectById(doctor.getHospitalId());
            doctorBO.setHospitalName(hospital.getHospitalName());
            //查询科室名称
            TbDepartment department = departmentMapper.selectById(doctor.getDepartmentId());
            doctorBO.setDepartmentName(department.getDepartmentName());
            //查询职称
            TbPosition position = positionMapper.selectById(doctor.getPosition());
            doctorBO.setPositionName(position.getPosition());
            //查询职位
            TbLevel level = levelMapper.selectById(doctor.getLevel());
            doctorBO.setLevelname(level.getLevel());
            //判断是否开通问诊
            if (doctor.getInquiryStatus() == 1){
                doctorBO.setInquiryStatusName("开通");
            }else{
                doctorBO.setInquiryStatusName("未开通");
            }
            //是否能成为私人医生
            if (doctor.getPrivateStatus() == 0){
                doctorBO.setPrivateStatusName("可以成为私人医院");
            }else {
                doctorBO.setPrivateStatusName("不可以成为私人医院");
            }
            doctorBOList.add(doctorBO);
        }
        return doctorBOList;
    }
}